package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogService;
import com.itheima.service.BlogSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/classify")
public class ClassifyController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private BlogSortService blogSortService;

    @GetMapping("/getArticleByBlogSortUid")
    public Result getArticleByBlogSortUid(@RequestParam("blogSortUid") String blogSortUid,@RequestParam("currentPage") Long currentPage,@RequestParam("pageSize") Long pageSize){
        Map map = new HashMap();
        map.put("blogSortUid",blogSortUid);
        map.put("pageSize",pageSize);
        map.put("currentPage",currentPage);
        PageResult<Blog>list = blogService.getList(map);
        Map data = new HashMap();
        data.put("records",list);
        data.put("size",pageSize);
        data.put("total",list.getTotal());
        data.put("current",currentPage);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        return new Result("success",data);
    }

    @GetMapping("/getBlogSortList")
    public Result getBlogSortList(){
        List<BlogSort> blogSorts = blogSortService.getBlogSortList();
        return new Result("success",blogSorts);
    }
}
